(function(){
	var AjaxCrud = QualityCheck.AjaxCRUD;
	var serverUrl = QualityCheck.GPUD.ServiceConfig;
	$(function(){
		var usercode = $("#user-name",parent.parent.document).attr("code");
		var userkey = $("#user-name",parent.parent.document).attr("key");
		var usertype = $("#user-name",parent.parent.document).attr("usertype");
		keyCheck(usercode,usertype,userkey,function(){
			initSmPages();
			window.onresize = function () {
				initSmPages();
			};
			pagebtns();
			initTeaherMaillist();
		});
	});	
	function initTeaherMaillist(){
		$("#teacher-maillist-page").empty();
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var classcode = $(".classes-choice-select",parent.document).attr("classcode");		
		var obj = {
			schoolcode:schoolcode
		};
		if(classcode != "" && classcode != undefined){
			obj["classcode"] = classcode;
		}
		var data = {
				cmd:"getMaillistByClasscode",
				model:"maillist",
				userKey:userKey,
				data:obj,
				remark:"",
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			var content = "";
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var maoAHref = "索引： ";
				var data = res.data;
				if(data.length > 0){
					for(var i = 0; i < data.length; i++){
						var groupName = data[i].groupName;
						var group = data[i].group;
						if(group.length > 0){
							for(var j = 0; j < group.length; j++){
								if(j == 0){
									maoAHref += " <a href='#"+groupName+j+"' style=\"color:#fff;\"> " + groupName.toUpperCase() + " </a> ";
								}
								for(var key in group[j]){
									if(group[j][key] == null || group[j][key] == "null"){
										group[j][key] = "";
									}
								}	
								content += "<div class=\"table-trs\">" +
								"<div class=\"tables-left\" style='margin-left:15px;width:120px;'>" +
								"<div class=\"table-name\" style='width:220px;' id='"+group[j].groupName+j+"'>"+ group[j].name + "</div>" +
								"<div class=\"table-code\" style='width:220px;'>科目： "+ group[j].coursecontent +"</div>" +
								"</div>" +
								"<div class=\"tables-middle\">" +
								"<div class=\"table-class\" style='width:220px;'>手机："+group[j].cellphone+"</div>";
								if(group[j].phone == ""){
									content += "<div class=\"table-code\" style='width:220px;'>电话：暂无</div>";
								}else{
									content += "<div class=\"table-code\" style='width:220px;'>电话："+group[j].phone+"</div>";
								}
								content += "</div>" +
								"<div class=\"tables-right\">" +
								"<div class=\"table-address\"></div>" +
								"<div class=\"table-address\"></div>" +
								"</div>" +
								"</div>";
								$("#teacher-maillist-data-show").empty();
								$("#teacher-maillist-data-show").append(content);
								
								$("#teacher-maillist-page").empty();
								$("#teacher-maillist-page").append(maoAHref);
							}
						}else{
							content += "<div class=\"table-trs\">" +
										"<div class=\"no-data-show\">没有查到相关结果！</div>" +
										"</div>";
							$("#teacher-maillist-data-show").empty();
							$("#teacher-maillist-data-show").append(content);
						}
					}
				}else{
					content += "<div class=\"table-trs\">" +
								"<div class=\"no-data-show\">没有查到相关结果！</div>" +
								"</div>";
					$("#teacher-maillist-data-show").empty();
					$("#teacher-maillist-data-show").append(content);
				}
			}else{
				content += "<div class=\"table-trs\">" +
								"<div class=\"no-data-show\">没有查到相关结果！</div>" +
							"</div>";
				$("#teacher-maillist-data-show").empty();
				$("#teacher-maillist-data-show").append(content);
			}
		},null,null);	
	}

	function substr(str,len){
		if(str != undefined){
			if(str.length > len){
				str = str.substring(0,len-2)+"...";
				return str;
			}else{
				return str;
			}
		}else{
			return "";
		}
	}
	
	function pagebtns(){
		$(".classes-choice",parent.document).unbind("click");
		$(".classes-choice",parent.document).click(function(){
			$(".classes-choice",parent.document).removeClass("classes-choice-select");
			$(this).addClass("classes-choice-select");
			initTeaherMaillist();
			
		});	
	}
	function initSmPages(){
		var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
		$(".right-content").height(rcH);
		
		var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
		$(".content-data-show").height(cdsH);
	}
})();